package com.qxf.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.qxf.exception.UserConfigurationException;
import com.qxf.model.beans.ActivityBean;
import com.qxf.model.beans.UserSession;
import com.qxf.util.db.SQLProcessor;
import com.qxf.util.db.SQLUtil;

public class LoginDao {
	
	Logger log = Logger.getLogger(this.getClass());

	public UserSession login(String userName, String passwd) throws Exception {
		UserSession loginInfo = null;
		UserSession user = checkUser(userName, passwd);
		if(null != user) {
			loginInfo = getUserInfo(user);
		}
		return loginInfo;
	}
	
	@SuppressWarnings({ "rawtypes" })
	private UserSession checkUser(String userName, String password) throws Exception {
		SQLProcessor sp = null;
		String sql = null;
		List list = null;
		UserSession userInfo = null;
		try{
			sp = new SQLProcessor();
			sql = "select user_id,user_name,user_code from tb_user where user_name = " + SQLUtil.toSqlString(userName) + 
				" and user_password = " + SQLUtil.toSqlString(password);
			log.info(sql);
			list = sp.executeQuery(sql);
			if(list.size() == 0) {
				throw new UserConfigurationException("用户名或密码错误");
			}
			if(list.size() == 1) {
				Map map = (HashMap)list.get(0);
				userInfo = new UserSession();
				userInfo.setUserId(String.valueOf((Integer)map.get("user_id")));
				userInfo.setUserName((String)map.get("user_name"));
				userInfo.setUserCode((String)map.get("user_code"));
			}
		} catch(SQLException se){
			sp.rollback();
			se.printStackTrace();
			throw se;
		} finally{
			SQLProcessor.staticClose(sp);
		}
		return userInfo;
	}
	
	@SuppressWarnings("rawtypes")
	public UserSession getUserInfo(UserSession user) throws Exception {
		SQLProcessor sp = null;
		String sql = null;
		List list = null;
		UserSession userInfo = new UserSession();
		try{
			sp = new SQLProcessor();
			sql = "select u.user_id,u.user_name,u.user_code,a.activity_id,a.activity_name,a.activity_code,a.activity_url" +
					" from tb_activity a" +
					" left join tb_user u on a.user_id=u.user_id" +
					" where u.user_name=" + SQLUtil.toSqlString(user.getUserName());
			log.info(sql);
			list = sp.executeQuery(sql);
			ActivityBean activity = null;
			List<ActivityBean> activityList = new ArrayList<ActivityBean>();
			Map map = null;
			Iterator it = list.listIterator();
			while(it.hasNext()) {
				activity = new ActivityBean();
				map = (HashMap)it.next();
				activity.setActivityId(String.valueOf((Integer)map.get("activity_id")));
				activity.setActivityName((String)map.get("activity_name"));
				activity.setActivityCode((String)map.get("activity_code"));
				activity.setActivityUrl((String)map.get("activity_url"));
				activityList.add(activity);
			}
			userInfo.setUserId(user.getUserId());
			userInfo.setUserName(user.getUserName());
			userInfo.setUserCode(user.getUserCode());
			userInfo.setActivityInfo(new ActivityBean());
			userInfo.setActivityInfoList(activityList);
		}catch(SQLException se){
			sp.rollback();
			se.printStackTrace();
			throw se;
		}finally{
			SQLProcessor.staticClose(sp);
		}
		return userInfo;
	}
}
